global input "Q:\dc1prhcmsas01\PU2\data - sas"
global temp "Q:\dc1prhcmsas01\PU2\temp_stata_wr"
global log "Q:\dc1prhcmsas01\PU2\Log - Stata WR"
global output "Q:\dc1prhcmsas01\PU2\data_stata_wr"
global graphs "Q:\dc1prhcmsas01\PU2\graphs_stata_wr"
global tables "Q:\dc1prhcmsas01\PU2\tables_stata_wr"

cd "Q:\dc1prhcmsas01\PU2\Code - Stata WR"

set scheme plotplain
graph set window fontface "Times New Roman"

capture log close
log using "$log/17_firm_employment", replace

****************************************
*Define share of firm treated 
****************************************
	use client_id base0 salaried0 state yr_month if yr_month==mdy(4,1,2016) using "$output\unbalanced_panel_treat.dta", clear
		gen treat_sal=salaried0==1 & base0>=455 & base0<=913
		gen treat_all= base0>=455 & base0<=913
		gen treat_sal_sub=salaried0==1 & base0>=770 & base0<=913
		egen state_num=group(state)
		egen mode_state=mode(state_num), by(client_id)
		
	preserve 
		keep if salaried==1 
		collapse (mean) treat_sal treat_sal_sub, by(client_id)
		tempfile temp 
		save `temp'
	restore 
	
	collapse (mean) treat_sal_all=treat_sal treat_all treat_sub_all=treat_sal_sub mode_state, by(client_id)
	merge 1:1 client_id using `temp'
	drop _merge
	save "$temp/baseline_treated", replace
	
****************************************
*Plot share treated across firms 
****************************************
	histogram treat_sal, width(0.005) percent xtitle("Share of Salaried Workers Treated") ytitle("Percent of Firms") ylabel(0(5)25)
	graph export "$graphs/histogram_share_salaried_treated.eps", replace
	graph export "$graphs/histogram_share_salaried_treated.jpg", replace

	histogram treat_sal_all, width(0.005) percent xtitle("Share of All Workers Treated") ytitle("Percent of Firms") ylabel(0(5)30)
	graph export "$graphs/histogram_share_all_treated.eps", replace
	graph export "$graphs/histogram_share_all_treated.jpg", replace
	
	count 
	count if treat_sal==0
	count if treat_sal_all==0
	
	sum treat_sal, det
	sum treat_sal_all, det

	

****************************************
*Merge onto panel 
****************************************
	use "$output\unbalanced_distribution_treat.dta", clear
	gen bunched= n if bin==913 & salaried==1
	replace bunched = 0 if bunched==.
	gen n_salaried= n if salaried==1 
	gen base_salaried = base if salaried==1 
	
	*Collapse by firm-year
	gcollapse (sum) n base bunched n_salaried base_salaried, by(yr_month client_id)
	gen log_n=log(n)
	gen log_n_sal=log(n_salaried)
	gen log_n_hr=log(n-n_salaried)
	gen wage_bill=base/n
	gen log_bill=log(wage_bill)
	gen wage_bill_sal=base_salaried/n_salaried 
	gen log_bill_sal=log(wage_bill_sal)
	gen share_bunched = bunched/n
	gen time=mofd(yr_month)-mofd(mdy(4,01,2016))
	merge m:1 client_id using "$temp/baseline_treated"
	drop _merge
	merge m:1 client_id using "$output/client_crosswalk"
	drop _merge
	
****************************************
*Program to estimate regressions
****************************************
cap program drop reg_est
program define reg_est 
args treat var name sal

	*Estimate diff-in-diff
	preserve 
		rename mode_state state
		
		local index=1
		forvalues i=-11/25 {
			gen d_`index'= `treat'*time==`i'
			label var d_`index' "Treat=1 and Event Time=`i'"
			local ++index
		}
		drop d_12
		gen treatpost1 =  `treat'*(time>8)
		gen treatpost2 =  `treat'*(time>0 & time<=8)
		
		*********************
		*Baseline regression
		*********************
		reghdfe `var' d_*, absorb(client_id time) vce(cluster client_id)
		
		cap drop beta ub lb
		gen beta=.
		gen ub=.
		gen lb=.
		gen event_time=.
		gen spec=""
		gen se=.
		gen obs=.
			
		local index=1
		forvalues i=-11/25 {
			local j=`i'+12
			replace event_time=`i' if _n==`index'
			replace spec="Basic" if _n==`index'
			if `i'!=0 {
				replace beta=_b[d_`j'] if _n==`index'
				replace ub=_b[d_`j'] + 1.96*_se[d_`j'] if _n==`index'
				replace lb=_b[d_`j'] - 1.96*_se[d_`j'] if _n==`index'	
			}
			if `i'==0 {
				replace beta=0 if _n==`index'
				replace ub=0 if _n==`index'
				replace lb=0 if _n==`index'
			}
			local ++index
		}
		reghdfe `var' treatpost1 treatpost2, absorb(client_id time) vce(cluster client_id)
			replace spec="Basic1" if _n==`index'
			replace beta=_b[treatpost1] if _n==`index'
			replace se=_se[treatpost1] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="Basic2" if _n==`index'
			replace beta=_b[treatpost2] if _n==`index'
			replace se=_se[treatpost2] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
		
		*********************
		*State FE regression
		*********************
		reghdfe `var' d_*, absorb(state##client_id state##time) vce(cluster client_id)
			
		forvalues i=-11/25 {
			local j=`i'+12
			replace event_time=`i' if _n==`index'
			replace spec="State_FE" if _n==`index'
			if `i'!=0 {
				replace beta=_b[d_`j'] if _n==`index'
				replace ub=_b[d_`j'] + 1.96*_se[d_`j'] if _n==`index'
				replace lb=_b[d_`j'] - 1.96*_se[d_`j'] if _n==`index'	
			}
			if `i'==0 {
				replace beta=0 if _n==`index'
				replace ub=0 if _n==`index'
				replace lb=0 if _n==`index'
			}
			local ++index
		}
		reghdfe `var' treatpost1 treatpost2, absorb(state##client_id state##time) vce(cluster client_id)
			replace spec="State_FE1" if _n==`index'
			replace beta=_b[treatpost1] if _n==`index'
			replace se=_se[treatpost1] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="State_FE2" if _n==`index'
			replace beta=_b[treatpost2] if _n==`index'
			replace se=_se[treatpost2] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
		
		*********************
		*State NAICS regression
		*********************
		reghdfe `var' d_*, absorb(naics##state##client_id naics##state##time) vce(cluster client_id)
			
		forvalues i=-11/25 {
			local j=`i'+12
			replace event_time=`i' if _n==`index'
			replace spec="NAICS_FE" if _n==`index'
			if `i'!=0 {
				replace beta=_b[d_`j'] if _n==`index'
				replace ub=_b[d_`j'] + 1.96*_se[d_`j'] if _n==`index'
				replace lb=_b[d_`j'] - 1.96*_se[d_`j'] if _n==`index'	
			}
			if `i'==0 {
				replace beta=0 if _n==`index'
				replace ub=0 if _n==`index'
				replace lb=0 if _n==`index'
			}
			local ++index
		}
		reghdfe `var' treatpost1 treatpost2, absorb(naics##state##client_id naics##state##time) vce(cluster client_id)
			replace spec="NAICS_FE1" if _n==`index'
			replace beta=_b[treatpost1] if _n==`index'
			replace se=_se[treatpost1] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			replace spec="NAICS_FE2" if _n==`index'
			replace beta=_b[treatpost2] if _n==`index'
			replace se=_se[treatpost2] if _n==`index'
			replace obs=`e(N)' if _n==`index'
			local ++index
			
		keep if beta!=.
		keep event_time beta se ub lb spec obs
		
		*Save estimates
		save "$output/`var'_`name'_estimates.dta", replace
	restore
end
	
	*Effect on SALARIED employment
	reg_est treat_sal n_sal firm_sal_emp_sal_treat
	reg_est treat_sal_all n_sal firm_sal_emp_salall_treat
	reg_est treat_sub_all n_sal firm_sal_emp_suball_treat
	reg_est treat_all n_sal firm_sal_emp_all_treat
	
	reg_est treat_sal log_n_sal firm_sal_emp_sal_treat
	reg_est treat_sal_all log_n_sal firm_sal_emp_salall_treat
	reg_est treat_sub_all log_n_sal firm_sal_emp_suball_treat
	reg_est treat_all log_n_sal firm_sal_emp_all_treat
	
	reg_est treat_sal log_bill_sal firm_sal_bill_sal_treat
	reg_est treat_sal_all log_bill_sal firm_sal_bill_salall_treat
	reg_est treat_sub_all log_bill_sal firm_sal_bill_suball_treat
	reg_est treat_all log_bill_sal firm_sal_bill_all_treat
	
	
	*Repeat using TOTAL employment as the outcome	
	reg_est treat_sal n firm_all_emp_sal_treat
	reg_est treat_sal_all n firm_all_emp_salall_treat
	reg_est treat_sub_all n firm_all_emp_suball_treat
	reg_est treat_all n firm_all_emp_all_treat	
	
	reg_est treat_sal log_n firm_all_emp_sal_treat
	reg_est treat_sal_all log_n firm_all_emp_salall_treat
	reg_est treat_sub_all log_n firm_all_emp_suball_treat
	reg_est treat_all log_n firm_all_emp_all_treat	
	
	reg_est treat_sal log_bill firm_all_sal_bill_sal_treat
	reg_est treat_sal_all log_bill firm_all_sal_bill_salall_treat
	reg_est treat_sub_all log_bill firm_all_sal_bill_suball_treat
	reg_est treat_all log_bill firm_all_sal_bill_all_treat
	
	*Effect on share of workers bunched at 913
	reg_est treat_sal bunched firm_all_sal_bunched_sal_treat
	reg_est treat_sal_all bunched firm_all_sal_bunched_salall_treat
	reg_est treat_sub_all bunched firm_all_sal_bunched_suball_treat
	reg_est treat_all bunched firm_all_sal_bunched_all_treat

	reg_est treat_sal share_bunched firm_all_sal_share_bunched_sal_treat
	reg_est treat_sal_all share_bunched firm_all_sal_share_bunched_salall_treat
	reg_est treat_sub_all share_bunched firm_all_sal_share_bunched_suball_treat
	reg_est treat_all share_bunched firm_all_sal_share_bunched_all_treat
	
***********************************************
*Plot avg. change in outcomes by share affected
***********************************************
	preserve 
		keep if time==0
		keep client_id n wage_bill 
		rename n n0 
		rename wage_bill wage_bill0
		tempfile temp 
		save `temp'
	restore
	merge m:1 client_id using `temp'
	drop _merge
	gen change_n = (n-n0)/n0
	gen change_wage = (wage_bill-wage_bill0)/wage_bill0
	
	*Plot post
	binscatter change_n treat_sal_all if time==25, xtitle("Share of Workers Affected") ytitle("Percent Change in Employment")
	graph export "$graphs/binscatter_emp_treat_post.eps", replace
	graph export "$graphs/binscatter_emp_treat_post.jpg", replace
	
	binscatter change_wage treat_sal_all if time==25, xtitle("Share of Workers Affected") ytitle("Percent Change in Wages")
	graph export "$graphs/binscatter_wage_treat_post.eps", replace
	graph export "$graphs/binscatter_wage_treat_post.jpg", replace

	binscatter share_bunched treat_sal_all if time==25, xtitle("Share of Workers Affected") ytitle("Percent Change in Share Bunched")
	graph export "$graphs/binscatter_share_bunched_treat_post.eps", replace
	graph export "$graphs/binscatter_share_bunched_treat_post.jpg", replace
	
	*Plot pre
	binscatter change_n treat_sal_all if time==-11, xtitle("Share of Workers Affected") ytitle("Percent Change in Employment")
	graph export "$graphs/binscatter_emp_treat_pre.eps", replace
	graph export "$graphs/binscatter_emp_treat_pre.jpg", replace
	
	binscatter change_wage treat_sal_all if time==-11, xtitle("Share of Workers Affected") ytitle("Percent Change in Wages")
	graph export "$graphs/binscatter_wage_treat_pre.eps", replace
	graph export "$graphs/binscatter_wage_treat_pre.jpg", replace	

	binscatter share_bunched treat_sal_all if time==-11, xtitle("Share of Workers Affected") ytitle("Percent Change in Share Bunched")
	graph export "$graphs/binscatter_share_bunched_treat_pre.eps", replace
	graph export "$graphs/binscatter_share_bunched_treat_pre.jpg", replace
	

	binscatter share_bunched treat_sal if time==-11, xtitle("Share of Workers Affected") ytitle("Percent Change in Share Bunched")
	
	
log close
	